﻿@inject DialogService DialogService
@inject RBACService RBACService

<RadzenStack Gap="1rem">
        <RadzenStack Gap="1rem">
            <RadzenStack class="rz-p-0" Orientation="Orientation.Vertical" 
                                        AlignItems="AlignItems.Normal" 
                                        JustifyContent="JustifyContent.Normal" 
                                        Style="height: 80%">
            <RadzenCheckBoxList Data="@roles"
                                    @bind-Value=@values
                                    TValue="long"
                                    TextProperty="Name"
                                    ValueProperty="Id"
                                    AllowSelectAll="true"
                                    SelectAllText="全选"
                                    Orientation="Orientation.Vertical">
                </RadzenCheckBoxList>
            </RadzenStack>
            <RadzenStack Gap="1rem" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End" Style="height: 20px;">
                <RadzenButton ButtonStyle="ButtonStyle.Secondary" Text="取消" Click="OnCancel"/>
                <RadzenButton ButtonStyle="ButtonStyle.Primary" Text="保存"  Click="OnSave" />
            </RadzenStack>
        </RadzenStack>
</RadzenStack>

@code {
    [Parameter] public User PUser { get; set; } = default!;
    List<Role> roles { get; set; } = default!;
    IEnumerable<long> values = default!;

    private void OnSave()
    {
        try{
            if(values.Count() == 0){
                return;
            }
            var userinput = new UserRoleInput(){
                UserId = PUser.Id,
                RoleIds = values            
            };
            RBACService.GiveUserRole(userinput); 
            DialogService.Close();
        }
        catch(Exception ex){
            Console.WriteLine(ex.Message);
        }
    }

    private void OnCancel()
    {
        DialogService.Close();
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var selectedRoles = RBACService.GetRoleByUserId(PUser.Id);
        values = selectedRoles.Select(s => s.Id);
        roles = RBACService.GetRole(new SimpleInput("")).ToList();
    }
}

